package moon.excel.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import moon.excel.CellRegion;

/***
 * excelsheet的抽象表示
 * 
 * @author moon
 * 
 */
public class ExcelSheet {
	private String name;
	private int headBeginRowIndex = 0;
	private int headEndRowIndex = 0;
	private int footBeginRowIndex = 0;
	private int footEndRowIndex = 0;
	private int columnBeginIndex = 0;
	private List<? extends Object> beans = new ArrayList<Object>();
	private List<String[]> tableHeads = new ArrayList<String[]>();
	private Object[] attrNames;
	private Object[] tablefoot;
	private List<CellRegion> cellRegions = new ArrayList<CellRegion>();
	@SuppressWarnings("rawtypes")
	private IRowInfoFillInterceptor rowInfoFillInterceptor;
	private Map<Integer, String> infoFormatMap = new HashMap<Integer, String>();
	private Map<Integer, String> tableFootFormatMap = new HashMap<Integer, String>();

	public ExcelSheet() {
		super();
	}

	public ExcelSheet(String name) {
		super();
		this.name = name;
	}

	public Class<? extends Object> getBeanClass() {
		return beans.isEmpty() ? null : beans.get(0).getClass();
	}

	public List<? extends Object> getBeans() {
		return beans;
	}

	public Object[] getAttrNames() {
		return attrNames;
	}

	/***
	 * 需要导出的属性名数组。支持以下属性命名方式 <br>
	 * 1.对象调用链 a.b.c的方式获取嵌套属性值 不支持集合属性 <br>
	 * 2.attr1{字符串}attr2 用{}中的字符串连接属性值 <br>
	 * 3.IAttrHandle 自定义处理属性值 <br>
	 * eg. <br>
	 * abc$date.formatDate(a.b.c,{yyyyMMdd}){连接}d.e.f &nbsp;&nbsp;结果:abc20120102链接def
	 * 
	 * @param attrNames
	 */
	public void setAttrNames(Object[] attrNames) {
		this.attrNames = attrNames;
	}

	public List<String[]> getTableHeads() {
		return tableHeads;
	}

	/***
	 * 表头的列名称，至少一行。
	 * 
	 * @param tableheads
	 */
	public void setTableHeads(List<String[]> tableheads) {
		this.tableHeads = tableheads;
		headEndRowIndex = tableheads.size() - 1;
	}

	/**
	 * 添加表头
	 * 
	 * @param tablehead
	 */
	public void addTableHead(String[] tablehead) {
		this.tableHeads.add(tablehead);
		headEndRowIndex = this.tableHeads.size() - 1;
	}

	/**
	 * 需要导出的数据bean
	 * 
	 * @param beans
	 */
	public void setBeans(List<? extends Object> beans) {
		this.beans = beans;
		footBeginRowIndex = beans.size() + headEndRowIndex + 1;
	}

	public int getColumnCount() {
		return tableHeads.get(0).length;
	}

	public Object[] getTablefoot() {
		return tablefoot;
	}

	/***
	 * 设置表尾
	 * 
	 * @param tablefoot
	 */
	public void setTablefoot(Object[] tablefoot) {
		this.tablefoot = tablefoot;
		footEndRowIndex = footBeginRowIndex + 1;
	}

	public List<CellRegion> getCellRegions() {
		return cellRegions;
	}

	public void setCellRegions(List<CellRegion> cellRegions) {
		this.cellRegions = cellRegions;
	}

	/**
	 * 添加合并区域
	 * 
	 * @param cellRegion
	 */
	public void addCellRegions(CellRegion cellRegion) {
		this.cellRegions.add(cellRegion);
	}

	/**
	 * 默认为0
	 * 
	 * @param headBeginRowIndex
	 */
	public void setHeadBeginRowIndex(int headBeginRowIndex) {
		this.headBeginRowIndex = headBeginRowIndex;
	}

	public int getHeadBeginRowIndex() {
		return headBeginRowIndex;
	}

	public int getHeadEndRowIndex() {
		return headEndRowIndex;
	}

	/**
	 * 默认为数据结束行的下一行
	 * 
	 * @param footBeginRowIndex
	 */
	public void setFootBeginRowIndex(int footBeginRowIndex) {
		this.footBeginRowIndex = footBeginRowIndex;
	}

	public int getFootBeginRowIndex() {
		return footBeginRowIndex;
	}

	public int getFootEndRowIndex() {
		return footEndRowIndex;
	}

	/**
	 * 导入时必填
	 * 
	 * @param headEndRowIndex
	 */
	public void setHeadEndRowIndex(int headEndRowIndex) {
		this.headEndRowIndex = headEndRowIndex;
	}

	public int getColumnBeginIndex() {
		return columnBeginIndex;
	}

	public void setColumnBeginIndex(int columnBeginIndex) {
		this.columnBeginIndex = columnBeginIndex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@SuppressWarnings("rawtypes")
	public IRowInfoFillInterceptor getRowInfoFillInterceptor() {
		return rowInfoFillInterceptor;
	}

	@SuppressWarnings("rawtypes")
	public void setRowInfoFillInterceptor(IRowInfoFillInterceptor rowInfoFillInterceptor) {
		this.rowInfoFillInterceptor = rowInfoFillInterceptor;
	}

	/**
	 * 设置数据列单元格显示格式
	 * 
	 * @param columns
	 * @param format
	 */
	public void setInfoColumnFormat(String columns, String format) {
		if (StringUtils.isNotBlank(columns) && StringUtils.isNotBlank(format)) {
			for (String c : columns.split(",")) {
				int column = NumberUtils.toInt(c, -1);
				if (column > -1) {
					infoFormatMap.put(column, format);
				}
			}
		}
	}

	/**
	 * 设置表尾列单元格显示格式
	 * 
	 * @param columns
	 * @param format
	 */
	public void setTableFootColumnFormat(String columns, String format) {
		if (StringUtils.isNotBlank(columns) && StringUtils.isNotBlank(format)) {
			for (String c : columns.split(",")) {
				int column = NumberUtils.toInt(c, -1);
				if (column > -1) {
					tableFootFormatMap.put(column, format);
				}
			}
		}
	}

	public String[] getInfoFormats() {
		if (infoFormatMap.isEmpty()) {
			return null;
		}
		String[] r = new String[getColumnCount()];
		for (int i = 0; i < getColumnCount(); i++) {
			r[i] = infoFormatMap.get(i);
		}
		return r;
	}

	public String[] getTableFootFormats() {
		if (tableFootFormatMap.isEmpty()) {
			return null;
		}
		String[] r = new String[getColumnCount()];
		for (int i = 0; i < getColumnCount(); i++) {
			r[i] = tableFootFormatMap.get(i);
		}
		return r;
	}
}